<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true">
            <el-form-item label="渠道名称" prop="channelName">
                <el-input v-model="queryParams.channelName" placeholder="请输入渠道名称" clearable style="width: 160px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="渠道类型" prop="type">
                <el-select v-model="queryParams.type" placeholder="渠道类型" clearable style="width: 160px">
                    <el-option v-for="dict in channel_type" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="渠道状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="渠道状态" clearable style="width: 160px">
                    <el-option v-for="dict in channel_status" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="创建时间" style="width: 308px">
                <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                    start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd">新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate">修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport">导出
                </el-button>
            </el-col>
        </el-row>

        <!-- 表格 -->
        <el-table v-loading="loading" :data="channelList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="渠道编号" prop="channelId" width="120" />
            <el-table-column label="渠道名称" prop="channelName" :show-overflow-tooltip="true" width="150" />
            <el-table-column label="渠道类型" :show-overflow-tooltip="true" width="100">
                <template #default="scope">
                    <template v-for="(type, indexi) in channel_type">
                        <el-tag :key="indexi" v-if="type.value == scope.row.type"
                            :type="type.elTagType">{{ type.label }}</el-tag>
                    </template>
                </template>
            </el-table-column>
            <el-table-column label="渠道状态" :show-overflow-tooltip="true" width="100">
                <template #default="scope">
                    <template v-for="(status, indexi) in channel_status">
                        <el-tag :key="indexi" v-if="status.value == scope.row.status"
                            :type="status.elTagType">{{ status.label }}</el-tag>
                    </template>
                </template>
            </el-table-column>
            <el-table-column label="创建人" prop="createBy" :show-overflow-tooltip="true" width="100" />
            <el-table-column label="创建时间" :show-overflow-tooltip="true" width="200">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="备注信息" prop="remark" :show-overflow-tooltip="true" width="180" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                        <el-button type="text" size="default" icon="Edit" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button type="text" size="default" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加修改渠道模态框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="channelRef" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="渠道名称" prop="channelName">
                    <el-input v-model="form.channelName" placeholder="请输入渠道名称" />
                </el-form-item>
                <el-form-item label="渠道状态" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio v-for="dict in channel_status" :key="dict.value" :label="parseInt(dict.value)">{{
                            dict.label
                            }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="渠道类型" prop="type">
                    <el-radio-group v-model="form.type">
                        <el-radio v-for="dict in channel_type" :key="dict.value" :label="parseInt(dict.value)">{{
                            dict.label }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>

    </div>
</template>

<script setup name="channel" lang="ts">
import { useRouter } from 'vue-router';
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';
import { getToken } from "../../../utils/auth";
import { listChannel, addChannel, getChannel, updateChannel, delChannel } from "../../../api/tienchin/channel";

const router = useRouter();
const { proxy } = getCurrentInstance();
const { channel_type, channel_status } = proxy.useDict("channel_type", "channel_status");

const channelList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        channelName: undefined,
        type: undefined,
        status: undefined
    },
    rules: {
        channelName: [{ required: true, message: "渠道名称不能为空", trigger: "blur" }],
        status: [{ required: true, message: "渠道状态不能为空", trigger: "blur" }],
        type: [{ required: true, message: "渠道类型不能为空", trigger: "blur" }]
    },
});

const { queryParams, form, rules } = toRefs(data);

/* 查询列表 */
function getList() {
    loading.value = true;
    listChannel(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
        channelList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

/* 搜索 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/* 重置 */
function resetQuery() {
    dateRange.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

/* 删除 */
function handleDelete(row) {
    const channelIds = row.channelId || ids.value;
    proxy.$modal.confirm('是否确认删除渠道编号为"' + channelIds + '"的数据项?').then(function () {
        return delChannel(channelIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {
    });
}

/* 导出 */
function handleExport() {
    proxy.download("tienchin/channel/export", {
        ...queryParams.value,
    }, `channel_${new Date().getTime()}.xlsx`);
}

/* 多选 */
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.channelId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/* 重置表单 */
function reset() {
    form.value = {
        channelName: undefined,
        status: undefined,
        type: undefined,
        remark: undefined
    };
    proxy.resetForm("channelRef");
}

/* 添加 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加渠道";
}

/* 修改 */
function handleUpdate(row) {
    reset();
    const channelId = row.channelId || ids.value;
    getChannel(channelId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改渠道";
    });
}

/* 提交 */
function submitForm() {
    proxy.$refs["channelRef"].validate(valid => {
        if (valid) {
            if (form.value.channelId != undefined) {
                delete form.value.updateTime
                delete form.value.createTime
                updateChannel(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addChannel(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 取消 */
function cancel() {
    open.value = false;
    reset();
}

getList();
</script>
